package com.wangp.mywrite.inwork.sync;

/***
 * @author wangpeng
 * @since 2022-02-18  15:25
 */
public class SyncTest {


    /**
     * 与function2相互锁住
     *
     * @throws InterruptedException
     */
    public synchronized void function1() throws InterruptedException {
        System.out.println("function1...");
        Thread.sleep(1000);
    }

    public synchronized void function2() throws InterruptedException {
        System.out.println("function2...");
        Thread.sleep(1000);
    }



    public static void main(String[] args) throws InterruptedException {
        SyncTest syncTest = new SyncTest();
        Thread t1 = new Thread(() -> {
            try {
                syncTest.function1();
            } catch (InterruptedException e) {
            }
        });
        Thread t2 = new Thread(()->{
            try {
                syncTest.function2();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        t1.start();
        t2.start();
        t1.join();
        t2.join();

    }

}
